#include <Arduino.h>
#include <WiFi.h>
#include <Wire.h>
#include "time.h"
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
#define SCREEN_ADDRESS 0x3C

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

const char* ssid = "ESP32c3.10"; // 请替换为您的WiFi网络名称
const char* password = "000000000"; // 请替换为您的WiFi密码

const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 8 * 3600;
const int daylightOffset_sec = 0;

void setup() {
  Serial.begin(115200);
  Wire.begin(1,3);
  display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);
  display.display(); // 显示屏幕欢迎信息
  delay(2000);
  display.clearDisplay(); // 清除屏幕内容

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  Serial.println("Connected to WiFi");

  configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
}

void loop() {
  String currentDate = getCurrentDate();
  String currentDay = getCurrentDay();
  String currentTime = getCurrentTime();

  display.clearDisplay(); // 清除屏幕内容
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(0, 0);
  display.print("From Code to Object\nGroup 10\n");
  display.print("Wish You All The Best\n");
  display.print("Date:");
  display.println(currentDate);
  display.print("Week:");
  display.println(currentDay);
  display.print("Time:");
  display.println(currentTime);
  display.print("Have a nice time!");
  display.display(); // 显示内容
  delay(1000);
}

String getCurrentDate() {
  time_t now = time(nullptr);
  struct tm* timeInfo;
  timeInfo = localtime(&now);
  char buffer[30];
  strftime(buffer, sizeof(buffer), "%Y-%m-%d", timeInfo);
  return String(buffer);
}

String getCurrentDay() {
  time_t now = time(nullptr);
  struct tm* timeInfo;
  timeInfo = localtime(&now);
  char buffer[20];
  strftime(buffer, sizeof(buffer), "%A", timeInfo);
  return String(buffer);
}

String getCurrentTime() {
  time_t now = time(nullptr);
  struct tm* timeInfo;
  timeInfo = localtime(&now);
  char buffer[20];
  strftime(buffer, sizeof(buffer), "%H:%M:%S", timeInfo);
  return String(buffer);
}
